<template>
  <a-card :bordered="false">
    <a-space :size="54">
      <a-upload
        :custom-request="customRequest"
        list-type="picture-card"
        :file-list="fileList"
        :show-upload-button="true"
        :show-file-list="false"
        @change="uploadChange"
      >
        <template #upload-button>
          <a-avatar :size="100" class="info-avatar">
            <template #trigger-icon>
              <icon-camera />
            </template>
            <img v-if="fileList.length" :src="fileList[0].url" />
          </a-avatar>
        </template>
      </a-upload>
      <a-row :gutter="32">
        <a-col :span="12">
          <label>用户名:</label>
          <span>{{ userInfo.nickName }}</span>
        </a-col>
        <a-col :span="12">
          <label>手机号码:</label>
          <span>{{ userInfo.phonenumber }}</span>
        </a-col>
        <a-col :span="12">
          <label>用户邮箱:</label>
          <span>{{ userInfo.email }}</span>
        </a-col>
        <a-col :span="12">
          <label>所属部门:</label>
          <span>{{ deptName }}</span>
        </a-col>
        <a-col :span="12">
          <label>所属角色:</label>
          <span>{{ userInfo.roleId }}</span>
        </a-col>
        <a-col :span="12">
          <label>创建日期:</label>
          <span>{{ userInfo.createTime }}</span>
        </a-col>
      </a-row>
      <!-- <a-descriptions
        :data="renderData"
        :column="2"
        align="right"
        layout="inline-horizontal"
        :label-style="{
          width: '140px',
          fontWeight: 'normal',
          color: 'rgb(var(--gray-8))',
        }"
        :value-style="{
          width: '200px',
          paddingLeft: '8px',
          textAlign: 'left',
        }"
      >
        <template #label="{ label }">{{ label }} :</template>
        <template #value="{ value }">
          <span>{{ value }}</span>
        </template>
      </a-descriptions> -->
    </a-space>
  </a-card>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { storeToRefs } from 'pinia';
  import type {
    FileItem,
    RequestOption,
  } from '@arco-design/web-vue/es/upload/interfaces';
  import { useUserStore } from '@/store';
  import { userUploadApi } from '@/api/user-center';
  import { Message } from '@arco-design/web-vue';

  const userStore = useUserStore();
  const { avatar, deptName, userInfo } = storeToRefs(userStore);
  const file = {
    uid: '-2',
    name: 'avatar.png',
    url: import.meta.env.VITE_API_BASE_URL + avatar?.value,
  };
  const fileList = ref<FileItem[]>([file]);
  const uploadChange = (fileItemList: FileItem[], fileItem: FileItem) => {
    fileList.value = [fileItem];
  };
  const customRequest = (options: RequestOption) => {
    // docs: https://axios-http.com/docs/cancellation
    const controller = new AbortController();

    (async function requestWrap() {
      const {
        onProgress,
        onError,
        onSuccess,
        fileItem,
        name = 'avatarfile',
      } = options;
      onProgress(20);
      const formData = new FormData();
      formData.append(name as string, fileItem.file as Blob);
      const onUploadProgress = (event: ProgressEvent) => {
        let percent;
        if (event.total > 0) {
          percent = (event.loaded / event.total) * 100;
        }
        onProgress(parseInt(String(percent), 10), event);
      };

      try {
        const res = await userUploadApi(formData, {
          controller,
          onUploadProgress,
        });
        if (res.code !== 200) {
          Message.error(res.msg);
          return;
        }
        userStore.info();
        onSuccess(res);
      } catch (error) {
        onError(error);
      }
    })();
    return {
      abort() {
        controller.abort();
      },
    };
  };
</script>

<style scoped lang="less">
  .arco-col {
    margin-bottom: 16px;
    label {
      color: rgb(var(--gray-8));
    }
    span {
      margin-left: 10px;
    }
  }
  .arco-card {
    padding: 14px 0 4px 4px;
    border-radius: 4px;
  }
  :deep(.arco-avatar-trigger-icon-button) {
    width: 32px;
    height: 32px;
    line-height: 32px;
    background-color: #e8f3ff;
    .arco-icon-camera {
      margin-top: 8px;
      color: rgb(var(--arcoblue-6));
      font-size: 14px;
    }
  }
</style>
